﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Experimental.Rendering.GraphicsFormatUtility
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Runtime.CompilerServices;
using UnityEngine.Bindings;
using UnityEngine.Rendering;

#nullable disable
namespace UnityEngine.Experimental.Rendering
{
  [NativeHeader("Runtime/Graphics/TextureFormat.h")]
  [NativeHeader("Runtime/Graphics/GraphicsFormatUtility.bindings.h")]
  [NativeHeader("Runtime/Graphics/Format.h")]
  public class GraphicsFormatUtility
  {
    private static readonly GraphicsFormat[] tableNoStencil = new GraphicsFormat[5]
    {
      GraphicsFormat.None,
      GraphicsFormat.D16_UNorm,
      GraphicsFormat.D16_UNorm,
      GraphicsFormat.D24_UNorm,
      GraphicsFormat.D32_SFloat
    };
    private static readonly GraphicsFormat[] tableStencil = new GraphicsFormat[5]
    {
      GraphicsFormat.None,
      GraphicsFormat.D24_UNorm_S8_UInt,
      GraphicsFormat.D24_UNorm_S8_UInt,
      GraphicsFormat.D24_UNorm_S8_UInt,
      GraphicsFormat.D32_SFloat_S8_UInt
    };

    [FreeFunction("GetTextureGraphicsFormat")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern GraphicsFormat GetFormat([NotNull("NullExceptionObject")] Texture texture);

    public static GraphicsFormat GetGraphicsFormat(TextureFormat format, bool isSRGB)
    {
      return GraphicsFormatUtility.GetGraphicsFormat_Native_TextureFormat(format, isSRGB);
    }

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern GraphicsFormat GetGraphicsFormat_Native_TextureFormat(
      TextureFormat format,
      bool isSRGB);

    public static TextureFormat GetTextureFormat(GraphicsFormat format)
    {
      return GraphicsFormatUtility.GetTextureFormat_Native_GraphicsFormat(format);
    }

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern TextureFormat GetTextureFormat_Native_GraphicsFormat(GraphicsFormat format);

    public static GraphicsFormat GetGraphicsFormat(RenderTextureFormat format, bool isSRGB)
    {
      return GraphicsFormatUtility.GetGraphicsFormat_Native_RenderTextureFormat(format, isSRGB);
    }

    [FreeFunction]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern GraphicsFormat GetGraphicsFormat_Native_RenderTextureFormat(
      RenderTextureFormat format,
      bool isSRGB);

    public static GraphicsFormat GetGraphicsFormat(
      RenderTextureFormat format,
      RenderTextureReadWrite readWrite)
    {
      bool flag = QualitySettings.activeColorSpace == ColorSpace.Linear;
      bool isSRGB = readWrite == RenderTextureReadWrite.Default ? flag : readWrite == RenderTextureReadWrite.sRGB;
      return GraphicsFormatUtility.GetGraphicsFormat(format, isSRGB);
    }

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern GraphicsFormat GetDepthStencilFormatFromBitsLegacy_Native(
      int minimumDepthBits);

    internal static GraphicsFormat GetDepthStencilFormat(int minimumDepthBits)
    {
      return GraphicsFormatUtility.GetDepthStencilFormatFromBitsLegacy_Native(minimumDepthBits);
    }

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern int GetDepthBits(GraphicsFormat format);

    public static GraphicsFormat GetDepthStencilFormat(int minimumDepthBits, int minimumStencilBits)
    {
      if (minimumDepthBits == 0 && minimumStencilBits == 0)
        return GraphicsFormat.None;
      if (minimumDepthBits < 0 || minimumStencilBits < 0)
        throw new ArgumentException("Number of bits in DepthStencil format can't be negative.");
      if (minimumDepthBits > 32)
        throw new ArgumentException("Number of depth buffer bits cannot exceed 32.");
      if (minimumStencilBits > 8)
        throw new ArgumentException("Number of stencil buffer bits cannot exceed 8.");
      minimumDepthBits = minimumDepthBits > 16 ? (minimumDepthBits > 24 ? 32 : 24) : 16;
      if (minimumStencilBits != 0)
        minimumStencilBits = 8;
      Debug.Assert(GraphicsFormatUtility.tableNoStencil.Length == GraphicsFormatUtility.tableStencil.Length);
      GraphicsFormat[] graphicsFormatArray = minimumStencilBits > 0 ? GraphicsFormatUtility.tableStencil : GraphicsFormatUtility.tableNoStencil;
      for (int index = minimumDepthBits / 8; index < graphicsFormatArray.Length; ++index)
      {
        GraphicsFormat format = graphicsFormatArray[index];
        if (SystemInfo.IsFormatSupported(format, FormatUsage.Render))
          return format;
      }
      return GraphicsFormat.None;
    }

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsSRGBFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsSwizzleFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern GraphicsFormat GetSRGBFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern GraphicsFormat GetLinearFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern RenderTextureFormat GetRenderTextureFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern uint GetColorComponentCount(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern uint GetAlphaComponentCount(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern uint GetComponentCount(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern string GetFormatString(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsCompressedFormat(GraphicsFormat format);

    [FreeFunction("IsAnyCompressedTextureFormat", true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern bool IsCompressedTextureFormat(TextureFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern bool CanDecompressFormat(GraphicsFormat format, bool wholeImage);

    internal static bool CanDecompressFormat(GraphicsFormat format)
    {
      return GraphicsFormatUtility.CanDecompressFormat(format, true);
    }

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsPackedFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool Is16BitPackedFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern GraphicsFormat ConvertToAlphaFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsAlphaOnlyFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsAlphaTestFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool HasAlphaChannel(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsDepthFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsStencilFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsIEEE754Format(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsFloatFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsHalfFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsUnsignedFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsSignedFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsNormFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsUNormFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsSNormFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsIntegerFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsUIntFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsSIntFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsXRFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsDXTCFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsRGTCFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsBPTCFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsBCFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsPVRTCFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsETCFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsEACFormat(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool IsASTCFormat(GraphicsFormat format);

    public static bool IsCrunchFormat(TextureFormat format)
    {
      return format == TextureFormat.DXT1Crunched || format == TextureFormat.DXT5Crunched || format == TextureFormat.ETC_RGB4Crunched || format == TextureFormat.ETC2_RGBA8Crunched;
    }

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern FormatSwizzle GetSwizzleR(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern FormatSwizzle GetSwizzleG(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern FormatSwizzle GetSwizzleB(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern FormatSwizzle GetSwizzleA(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern uint GetBlockSize(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern uint GetBlockWidth(GraphicsFormat format);

    [FreeFunction(IsThreadSafe = true)]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern uint GetBlockHeight(GraphicsFormat format);

    public static uint ComputeMipmapSize(int width, int height, GraphicsFormat format)
    {
      return GraphicsFormatUtility.ComputeMipmapSize_Native_2D(width, height, format);
    }

    [FreeFunction]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern uint ComputeMipmapSize_Native_2D(
      int width,
      int height,
      GraphicsFormat format);

    public static uint ComputeMipmapSize(int width, int height, int depth, GraphicsFormat format)
    {
      return GraphicsFormatUtility.ComputeMipmapSize_Native_3D(width, height, depth, format);
    }

    [FreeFunction]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern uint ComputeMipmapSize_Native_3D(
      int width,
      int height,
      int depth,
      GraphicsFormat format);
  }
}
